/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jxtafileshare;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeID;

import java.net.UnknownHostException;
import java.net.InetAddress;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import net.jxta.document.*;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.TextDocumentMessageElement;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;

/**
 *
 * @author Danny
 */
public class Main extends JFrame {
    private static GroupCreator rv = null;
    private static FindGroup ed = null;
    private static PeerGroup group = null;
    private static NetworkStarter starter = null;
    private static ShareFiles sf;
    private static String wait = "";
    private JButton sendButton;
    private JButton searchButton;

    public static void main(String args[]) {
        Main main = new Main();
        
        main.addWindowListener(
                new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        starter = new NetworkStarter();
        starter.startNetwork();
        if(starter.getMode() == net.jxta.platform.NetworkManager.ConfigMode.RENDEZVOUS) {
            rv = new GroupCreator(starter.getNetworkManager());
            group = rv.getPeerGroup();
            wait = "false";
            GroupAdvertisor gAdv = new GroupAdvertisor(rv.getNetPeerGroup(), rv.getGroupAdvertisement());
            try {
                Thread.sleep(10000);
            } catch(Exception e) {
                // ignored
            }
            gAdv.start();
        } else {
            ed = new FindGroup(starter.getNetworkManager());
            group = ed.getDiscoveredPeerGroup();
            wait = "true";
        }

        sf = new ShareFiles(starter.getNetworkManager(), group);
    }
    
    public Main() {
        super("Fileshare");
        
        Container c = getContentPane();
        
        sendButton = new JButton("Share Files");
        sendButton.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FileFinder ff = new FileFinder();
                if(ff.findFiles()) {
                    sf.share(wait, ff.getFiles());
                } else {
                    System.out.println("No files on local directory");
                }
            }
        });
        c.add(sendButton, BorderLayout.NORTH);

        searchButton = new JButton("Send Search");
        searchButton.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sf.search();
            }
        });
        c.add(searchButton, BorderLayout.CENTER);

        setSize(300, 150);
        setVisible(true);
    }
}
